/**
 * @file STL_Mutex.hpp
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 线程锁
 * @version 0.0.1
 * @date 2020-07-16
 * 
 * @coryright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */
#pragma once
#include <pthread.h>

namespace stl
{

    class Mutex
    {
    public:
        Mutex();
        ~Mutex();

        int lock();

        int unlock();

        /**
     * @brief 尝试加锁
     * 
     * @return int 0: mutex未加锁直接返回 EBUSY: mutex已加锁
     */
        int trylock();

    private:
        pthread_mutex_t m_mtx;
    };

    class Guard
    {
    public:
        Guard(Mutex &m);
        ~Guard();

    private:
        Mutex &m_mtx;
    };

} // namespace stl